package com.thread.thread5;

import java.util.UUID;
import java.util.concurrent.*;

/**
 * @author : leimingchao
 * @date Date : 2021年11月30日 20:38
 */
public class CompletableFutureTest2 {


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        System.out.println("主线程开始。。。。。。。。");
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程开始：" + Thread.currentThread());
            String s = UUID.randomUUID().toString();
            System.out.println("当前线程结束：" + Thread.currentThread());
            return s;
        }, threadPool).thenApply(r -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
            }
            System.out.println("上一步的结果是：" + r);
            return r.replace("-","");
        }).whenComplete((r,e) -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e1) {
            }
            System.out.println("最终结果。。。。。" + r);
        });
        System.out.println("主线程结束。。。。。。。。" + completableFuture.get());
        threadPool.shutdown();
    }
}
